package com.web.exiaodao.remote.controller;

import com.web.exiaodao.remote.service.ApiServiceCommonService;
import com.web.exiaodao.remote.vo.ServiceCommonVo;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.controller.BaseController;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.jpa.WebUitl;
import com.web.webstart.base.util.result.XaResult;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author zhanglin
 * @version V1.0
 * @Title: ApiService_commonController.java
 * @Package com.web.exiaodao.remote.controller
 * @Description: 服务-公用表(1:金融保险 2：智慧传承 3：生前契约)信息接口
 * @date 2016-11-28 18:16:50
 */
@Api(value = "ServiceCommon", description = "服务(1:金融保险 2：智慧传承 3：生前契约)", position = 10)
@Controller
@RequestMapping("/api/serviceCommon")
public class ApiServiceCommonController extends BaseController {

    private static final Logger LOGGER = Logger.getLogger(ApiServiceCommonController.class);

    @Autowired
    private ApiServiceCommonService service_commonService;

    /**
     * @return
     * @Title: findService_commonList
     * @Description: 查询所有服务-公用表信息
     */
    @ApiOperation(value = "查询所有服务-公用表", notes = "查询所有服务-公用表信息,当返回的code=1时，取出object进行显示,存放为数组对象")
    @ResponseBody
    @RequestMapping(value = "findService_commonList", method = RequestMethod.POST)
    public XaResult<List<ServiceCommonVo>> findService_commonList(
            @ApiParam("类型,字段名:serviceType(1:金融保险 2：智慧传承 3：生前契约 4:医疗服务（医疗康复）)") @RequestParam(value = "serviceType") Integer serviceType,
            @ApiParam("页号,字段名:nextPage,默认0,从第0页开始") @RequestParam(defaultValue = "0") Integer nextPage,
            @ApiParam("页长,字段名:pageSize,默认10") @RequestParam(defaultValue = "10") Integer pageSize
    ) throws BusinessException {
        XaResult<List<ServiceCommonVo>> xr = new XaResult<List<ServiceCommonVo>>();
        if ( !(serviceType == 1 || serviceType == 2 || serviceType == 3 ||  serviceType == 4) ) {
            xr.error("serviceType取值错误", true);
            return xr;
        }
        Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, "[{property:'sort',direction:'ASC'},{property:'clickCont',direction:'DESC'},{property:'createTime',direction:'DESC'}]");
        Map<String, Object> filterParams   = WebUitl.getParametersStartingWith("{'search_EQ_serviceType':'" + serviceType + "'}", "search_");
        try {
            return service_commonService.findListEQStatusByFilter(XaConstant.Status.valid, filterParams, pageable);
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                xr.error(e.getMessage(), true);
            } else {
                xr.error(XaConstant.Message.error, true);
            }
            LOGGER.error(XaConstant.Message.error, new Throwable(e));
        }
        return xr;
    }


    /**
     * @return
     * @Title: findService_commonById
     * @Description: 根据ID查找单条实体
     * //@param modelId
     */
    @ApiOperation(value = "服务-公用表详情", notes = "查询服务-公用表详细信息,当返回的code=1时，取出object进行显示,存放为单体对象")
    @ResponseBody
    @RequestMapping(value = "findService_commonById", method = RequestMethod.POST)
    public XaResult<ServiceCommonVo> findService_commonById(
            @ApiParam("编号,字段名:id,必填") @RequestParam(value = "id") Long id
    ) throws BusinessException {
        XaResult<ServiceCommonVo> xr = new XaResult<ServiceCommonVo>();
        return service_commonService.findOne(id);
    }
}

